<?php
class MediaPlayerWidget extends CWidget
{
    private $_id;
    public $scriptUrl;
    public $flashPlayer='player.swf';
    public $width=480;
    public $height=270;
    public $useAbsoluteUrl=false;
    
    /**
    * @var string
    */
    public $file;
    public $image;
    /**
    *  {@see http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/15995/jw-embedder-reference-guide#playlists}
    *  {@see http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/12536/configuration-options#playlistprop}
    * 
    * array(
    *   array(
    *       'file'=>'', 
    *       'image'=>'', 
    *       'duration'=>'', 
    *       'start'=>'', 
    *       'title'=>'', 
    *       'description'=>'', 
    *       'streamer'=>'', 
    *       'provider'=>'', 
    *       'levels'=>'', 
    *   ),
    * )
    * @var array
    */
    public $playlist=array();
    /**
    * {@see http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/15995/jw-embedder-reference-guide#levels}
    * 
    * array(
    *   array(
    *       'bitrate'=>'', 
    *       'file'=>'', 
    *       'width'=>'', 
    *   ),
    * )
    * @var array
    */
    public $levels=array();
    /**
    * {@see http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/15995/jw-embedder-reference-guide#plugins}
    * 
    * @var array
    */
    public $plugins=array();
    /**
    * {@see http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/12540/javascript-api-reference#Events}
    * 
    * @var array
    */
    public $events=array();
    /**
    * {@see http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/15995/jw-embedder-reference-guide#players}
    * 
    * @var boolean
    */
    public $usePlayers=true;
    
    public $options=array();
    
    public $htmlOptions=array();
    
    public function init()
    {
        $this->_id = $this->getId();
        $this->registerClientScript();
        parent::init();
    }
    
    public function run()
    {
        parent::run();
    }
    
    protected function registerClientScript()
    {
        if ($this->scriptUrl === null) {
            $this->scriptUrl = Yii::app()->getAssetManager()->publish(dirname(__FILE__).DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'jwplayer');
        }
        
        $htmlOptions = CMap::mergeArray(array('id'=>$this->_id), $this->htmlOptions);
        $id = $htmlOptions['id'];
        $options = array(
            'height'=>(int) $this->height,
            'width'=>(int) $this->width,
            'plugins'=>array(
                'viral-2'=>array(),
            ),
            'viral.onpause'=>false,
            'viral.oncomplete'=>false,
            'viral.allowmenu'=>false,
            'wmode'=>'opaque',
            'dock'=>true,
            'controlbar.idlehide'=>true,
            'icons'=>false,
        );
        
        if ($this->usePlayers) {
            $options['modes'] = array(
                array('type'=>'html5','config'=>array('provider'=>'video','file'=>'')),
                array('type'=>'flash', 'src'=>$this->scriptUrl.'/'.$this->flashPlayer),
                array('type'=>'download'),
            );
        } else
            $options['flashplayer'] = $this->scriptUrl.'/'.$this->flashPlayer;
            
        if (is_array($this->playlist) && count($this->playlist))
        {
            $options['playlist'] = $this->playlist;
            if ($this->useAbsoluteUrl)
                $options['playlist'] = Yii::app()->request->hostInfo.Yii::app()->baseUrl.'/'.$options['playlist'];
        }
        else {
            if ($this->useAbsoluteUrl && strpos($this->file, 'http://') === false)
            {
                $this->file = Yii::app()->request->hostInfo.Yii::app()->baseUrl.'/'.trim($this->file,'/');
            }
            $options['modes'][0]['config']['file'] = $options['file'] = $this->file;
            
            if (empty($this->image) === false)
            {
                $options['image'] = $this->image;
                if ($this->useAbsoluteUrl)
                    $options['image'] = Yii::app()->request->hostInfo.Yii::app()->baseUrl.'/'.$options['image'];
            }
        }
            
        if (is_array($this->levels) && count($this->levels))
            $options['levels'] = $this->levels;
            
        if (is_array($this->plugins) && count($this->plugins))
            $options['plugins'] = $this->plugins;
            
        if (is_array($this->events) && count($this->events))
            $options['events'] = $this->events;
            
        $options = CMap::mergeArray($options, $this->options);
        $options = CJavaScript::encode($options);        
        echo CHtml::tag('div', $htmlOptions);
        
        $cs = Yii::app()->clientScript;
        $script = "jwplayer('{$id}').setup({$options});";
        $cs->registerScript(__CLASS__.'#'.$id, $script);
        $cs->registerScriptFile($this->scriptUrl.'/jwplayer.js', CClientScript::POS_HEAD);
    }
}